package cn.cloud.jianghai.jianghaicommon.utils;


import cn.hutool.core.bean.BeanUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 拷贝工具类
 * @author Jiang.h
 * created 2022/5/22 12:09
 */
public class ConvertUtil implements Serializable {

    public static <T> List<T> copyListProperties(List<?> source, Class<T> cls) {

        List<T> result = new ArrayList<>();

        source.forEach(item ->  result.add(copyBeanProperties(item, cls)));

        return result;
    }


    public static <T> T copyBeanProperties(Object item, Class<T> cls) {

        try {
            T t = cls.getDeclaredConstructor().newInstance();

            BeanUtil.copyProperties(item, t);

            return t;
        } catch (Exception e) {
        }

        return null;
    }
}
